package com.vito.oa.act.service.impl;

import static com.vito.oa.act.constant.OaActContants.CACHE_ACT_VALUE;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;

import com.alibaba.dubbo.config.annotation.Service;
import com.vito.base.model.DataGrid;
import com.vito.base.model.Result;
import com.vito.base.msg.service.MsgYtxService;
import com.vito.oa.act.dao.OaActCommentMapper;
import com.vito.oa.act.extend.model.OaActExApproveInfoModel;
import com.vito.oa.act.model.OaActCommentAttentionModel;
import com.vito.oa.act.model.OaActCommentModel;
import com.vito.oa.act.service.OaActCommentService;

@Service
public class OaActCommentServiceImpl implements OaActCommentService {
	
	private static final Logger log = LoggerFactory.getLogger(OaActCommentServiceImpl.class);

	@Autowired
	private OaActCommentMapper oaActCommentMapper;
	
	@Autowired
	private MsgYtxService msgYtxService;
	
	@Override
	@CacheEvict(value=CACHE_ACT_VALUE, allEntries=true)
	public boolean saveComment(OaActCommentModel oaActCommentModel) {
		boolean b = false;
		b = oaActCommentMapper.saveComment(oaActCommentModel) > 0;
		//消息推送
		String content = oaActCommentModel.getUserName() + "的审批评论中有提到你！";
		if (StringUtils.isNotBlank(oaActCommentModel.getAttentionList())) {
			String [] attentions = oaActCommentModel.getAttentionList().split(",");
			String batchId = oaActCommentModel.getBatchId();
			for (int i = 0; i < attentions.length; i++) {
				OaActCommentAttentionModel model = new OaActCommentAttentionModel();
				model.setCommentId(batchId);
				model.setUserId(attentions[i]);
				sendMsg(oaActCommentModel.getCreateUser(), attentions[i], content, oaActCommentModel.getSender());
				b = oaActCommentMapper.saveCommentAttention(model) > 0;
			}
		}
		return b;
	}
	
	private boolean sendMsg(String userId, String msgReceiverUsers, String content, String sender) {
		boolean b = false;
		Map<String, String> map = new HashMap<String, String>();
		map.put("userId", userId);
		map.put("pushType", "1");
		map.put("msgType", "1");
		map.put("sender", sender);
		map.put("msgContent", content);
		map.put("receiver", msgReceiverUsers);
		map.put("msgFileName", "approve_comment_smg");
		Result r = msgYtxService.addImMsg(map);
		if (r.getCode() == Result.CODE_OK) {
			log.info("【审批评论】：消息推送成功");
			b = true;
		} else {
			log.error("【审批评论】：消息推送失败");
		}
		return b;
	}
	
	@Cacheable(value=CACHE_ACT_VALUE,key=OaActCommentModel.CACHE_KEY)
	public List<OaActCommentModel> getCommentList(OaActCommentModel oaActCommentModel) {
		return oaActCommentMapper.getCommentListByBusinessId(oaActCommentModel);
	}

	@Override
	@Cacheable(value=CACHE_ACT_VALUE,key=OaActExApproveInfoModel.CACHE_KEY)
	public DataGrid<Map<String, String>> getCommentApprovePage(OaActExApproveInfoModel actExApproveInfoModel) {
		DataGrid<Map<String, String>> data = new DataGrid<Map<String, String>>();
			data.setRows(oaActCommentMapper.getCommentApproveByPage(actExApproveInfoModel));
			data.setTotal(actExApproveInfoModel.getTotal());
		return data;
	}

}
